//
//  DetailTopCell.m
//  Cat
//
//  Created by lushitong on 2018/12/23.
//  Copyright © 2018年 lushitong. All rights reserved.
//

#import "DetailTopCell.h"
#import "ProductInfo.h"

#import "TYCyclePagerView.h"
#import "DetailsBannerCell.h"
#import "DetailsBannerView.h"
#import "TYPageControl.h"
#import "DetailsBannerView.h"

@interface DetailTopCell()<TYCyclePagerViewDataSource, TYCyclePagerViewDelegate>

@property (nonatomic, strong) TYCyclePagerView *pagerView;
@property (nonatomic, strong) NSMutableArray *bannerArray;
@property (nonatomic, strong) TYPageControl *pageControl;
@property (nonatomic, strong) DetailsBannerView *infoView;

@end

@implementation DetailTopCell

+ (CGSize)getTopCell {
	return CGSizeMake(ScreenWidth, ScreenWidth + kQKFitModuleDecimal(40.0));
}
- (void)setCellModelData:(id)cellModelData {
	ProductInfo *model = (ProductInfo *)cellModelData;
	self.bannerArray = model.imgs.mutableCopy;
	[self.infoView setCouponNumber:model.couponAmount withPrizeAmount:model.prizeAmount];
	self.pageControl.numberOfPages = model.imgs.count;
	[self.pagerView reloadData];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
	self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
	if (self) {
		self.cellSeparatorStyle = QKUITableViewCellSeparatorStyleSingleLineBottomZero;
		self.selectionStyle = UITableViewCellSelectionStyleNone;
		[self loadSubviews];
	}
	return self;
}

- (void)loadSubviews {
	[self.contentView addSubview:self.pagerView];
	[self.contentView addSubview:self.pageControl];
	[self.contentView addSubview:self.infoView];
	
	[self.pagerView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.height.equalTo(@([DetailTopCell getTopCell].height - kQKFitModuleDecimal(40.0)));
		make.left.and.right.and.top.equalTo(self.contentView);
	}];
	
	[self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
		make.centerX.equalTo(self.pagerView.mas_centerX);
		make.height.equalTo(@(kQKFitModuleDecimal(10)));
		make.bottom.equalTo(self.pagerView.mas_bottom).offset(-kQKFitModuleDecimal(6.0));
		make.width.equalTo(@(kQKFitModuleDecimal(100.0)));
	}];
	
	[self.infoView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.size.equalTo(CGSizeMake(ScreenWidth, kQKFitModuleDecimal(40)));
		make.left.and.bottom.equalTo(self.contentView);
	}];
}

- (NSInteger)numberOfItemsInPagerView:(TYCyclePagerView *)pageView {
	return self.bannerArray.count;
}

- (UICollectionViewCell *)pagerView:(TYCyclePagerView *)pagerView cellForItemAtIndex:(NSInteger)index {
	DetailsBannerCell *cell = [pagerView dequeueReusableCellWithReuseIdentifier:@"DetailsBannerCellID" forIndex:index];
	NSString *imageString = [self.bannerArray safeObjectAtIndex:index];
	cell.imageURLString = imageString;
	return cell;
}

- (TYCyclePagerViewLayout *)layoutForPagerView:(TYCyclePagerView *)pageView {
	TYCyclePagerViewLayout *layout = [[TYCyclePagerViewLayout alloc]init];
	layout.itemSize = [DetailTopCell getTopCell];
	layout.itemSpacing = kQKFitModuleDecimal(0.0);
	layout.itemHorizontalCenter = YES;
	layout.layoutType = TYCyclePagerTransformLayoutNormal;
	return layout;
}

// 滑动的处理办法
- (void)pagerView:(TYCyclePagerView *)pageView didScrollFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
	self.pageControl.currentPage = toIndex;
}

#pragma mark----- lazy init -----

- (TYCyclePagerView *)pagerView {
	if (!_pagerView) {
		_pagerView = [[TYCyclePagerView alloc] init];
		_pagerView.dataSource = self;
		_pagerView.delegate = self;
		_pagerView.isInfiniteLoop = NO;
		[_pagerView registerClass:[DetailsBannerCell class] forCellWithReuseIdentifier:@"DetailsBannerCellID"];
	}
	return _pagerView;
}

- (TYPageControl *)pageControl {
	if (!_pageControl) {
		_pageControl = [[TYPageControl alloc] init];
		_pageControl.currentPageIndicatorSize = CGSizeMake(kQKFitModuleDecimal(6), kQKFitModuleDecimal(6));
		_pageControl.pageIndicatorSize = CGSizeMake(kQKFitModuleDecimal(6), kQKFitModuleDecimal(6));
		_pageControl.currentPageIndicatorTintColor = COLOR_WITH_HEX(0xFFE8B5);
		_pageControl.pageIndicatorTintColor = COLOR_WITH_HEX_A(0xD8D8D8, 0.2);
		_pagerView.layer.masksToBounds = YES;
	}
	return _pageControl;
}

- (DetailsBannerView *)infoView {
	if (!_infoView) {
		_infoView = [[DetailsBannerView alloc] init];
	}
	return _infoView;
}
@end
